#include "dcs.h"
#include "i2c.h"
#include "printf.h"

//#if defined(_lvds_to_mipi_)||defined(_mipi_to_mipi_)||defined(_ttl_to_mipi_)

#define DELAY_CMD 0x00
//********************************//
// Initial const format:
//{cmd_type, data0, data1,.......};
// cmd_type  == DELAY_CMD, this cmd is to delay some time.
// cmd_type != DELAY_CMD, data_id = cmd_type
// general data_id:
// 0x05: DCS Short WRITE, no parameters
// 0x15: dDCS Short WRITE, 1 parameter
// 0x13: Generic Short WRITE, 1 parameter
// 0x23: Generic Short WRITE, 2 parameters
// 0x29: Generic Long Write
// 0x39: DCS Long Write/write_LUT Command Packet
// Max 122 Byte cmd can be sent each LPDT packet.

// example:
/*
{delay, 100};// delay 100ms
{0x05, 0x11}; //dsc cmd: exit_sleep_mode
{0x05, 0x29}; //dsc cmd: set_display_on
*/

/*
modify log:
code -> const
uint8_t -> unsigned char


*/

/**********************cmd write mode 0********************/

const u8 dcs0[]={0x32,0x01,0x00};
const u8 dcs1[]={0x29,0xFF,0x98,0x81,0x03};
const u8 dcs2[]={0x29,0xFF,0x98,0x81,0x04};
const u8 dcs3[]={0x29,0xFF,0x98,0x81,0x01};
const u8 dcs4[]={0x29,0xFF,0x98,0x81,0x00};

const u8 cmd_301[] = {0x05, 0x11, 0x00};
const u8 cmd_302[] = {0x05, 0x29, 0x00};

const u8 cmd_303[] = {0x05, 0x28, 0x00};
const u8 cmd_304[] = {0x05, 0x10, 0x00};

void Generic_Short_Write_1P(unsigned char data0, unsigned char data1)
{
	HDMI_WriteI2C_Byte(0xff, 0xd4);
	HDMI_WriteI2C_Byte(0x01, 0x0c);
	HDMI_WriteI2C_Byte(0x02, 0x04);
	HDMI_WriteI2C_Byte(0x03, 0x23);
	HDMI_WriteI2C_Byte(0x03, data0);
	HDMI_WriteI2C_Byte(0x03, data1);
	Timer0_Delay1ms(1);
	HDMI_WriteI2C_Byte(0x01, 0x00);
}

void DcsPktWrite(u8 DCS_DI, u8 Len, const u8 *Ptr)
{
	u8 i = 0;
	HDMI_WriteI2C_Byte(0xff, 0xd4);
	if (Len == 2) {
		HDMI_WriteI2C_Byte(0x01, 0x0c);
		HDMI_WriteI2C_Byte(0x02, 0x04);
		HDMI_WriteI2C_Byte(0x03, DCS_DI);
		HDMI_WriteI2C_Byte(0x03, *Ptr);
		HDMI_WriteI2C_Byte(0x03, *(Ptr + 1));
	} else {
		HDMI_WriteI2C_Byte(0x01, 0x0E);
		HDMI_WriteI2C_Byte(0x02, Len + 6);
		HDMI_WriteI2C_Byte(0x03, DCS_DI);
		HDMI_WriteI2C_Byte(0x03, Len);
		HDMI_WriteI2C_Byte(0x03, 0x00);

		for (i = 0; i < Len; i++) {
			HDMI_WriteI2C_Byte(0x03, *Ptr);
			Ptr++;
		}
	}
	Timer0_Delay1ms(1);
	HDMI_WriteI2C_Byte(0x01, 0x00);
}

void LPDT_Cmd_Write(const u8 *Ptr, u16 size)
{

	u8 i = 0;
	u8 cmd_type;

	cmd_type = *Ptr;

	if (cmd_type == DELAY_CMD) // delay
	{
		Ptr++;
		Timer0_Delay1ms(*Ptr);
	}

	else // LPDT write data
	{

		size = size / sizeof(u8);
		size--;
		// print("\r\n size = ");
		// printdec_u32(size);

		// HDMI_WriteI2C_Byte(0xff,0x81);  //reset dcs module
		// HDMI_WriteI2C_Byte(0x0e,0x6f);  //0xef
		// HDMI_WriteI2C_Byte(0x0e,0xff);
		HDMI_WriteI2C_Byte(0xff, 0xd4);
		// print("\r\n *Ptr = %x",*Ptr);

		if (size < 3) // short packet
		{
			HDMI_WriteI2C_Byte(0x01, 0x0c);
			HDMI_WriteI2C_Byte(0x02, 0x04);
			HDMI_WriteI2C_Byte(0x03, *Ptr); // data_id
		} else					// long packet
		{
			HDMI_WriteI2C_Byte(0x01, 0x0E);
			HDMI_WriteI2C_Byte(0x02, (u8)(size + 6));
			HDMI_WriteI2C_Byte(0x03, *Ptr);		    // data_id
			HDMI_WriteI2C_Byte(0x03, (u8)(size % 256)); // data lenth_L
			HDMI_WriteI2C_Byte(0x03, (u8)(size / 256)); // data lenth_H
		}
		Ptr++;
		for (i = 0; i < size; i++) {
			HDMI_WriteI2C_Byte(0x03, *Ptr);
			// print("\r\n *Ptr = %x",*Ptr);
			Ptr++;
		}

		if (size == 1) {
			HDMI_WriteI2C_Byte(0x03, 0x00);
		}
		HDMI_WriteI2C_Byte(0x01, 0x00);
		//	while(1);
	}
}
/*********************cmd write mode 0 end **************************/

/*********************cmd write mode 1******************************/

struct cmd {
	u8 data_id;
	u8 data1;
	int data2;
};

void CMD_write(u8 data_id, u8 data1, u8 data2)
{
	if (data_id != DELAY_CMD) {
		// dcs short packet
		// HDMI_WriteI2C_Byte( 0x01, 0x0c );
		// HDMI_WriteI2C_Byte( 0x02, 0x04 );
		HDMI_WriteI2C_Byte(0x03, data_id);
		HDMI_WriteI2C_Byte(0x03, data1);
		HDMI_WriteI2C_Byte(0x03, data2);
		// Timer0_Delay1ms(1);
	} else {
		Timer0_Delay1ms(data1);
		printf("\r\n lpdt delay");
	}
}

/*To reduce the time for LPDT write, only send 3 I2C cmd each LPDT, around 1.2ms each LPDT*/
void CMD_write_array(struct cmd *cmd, int size)
{
	u16 i;

	HDMI_WriteI2C_Byte(0x01, 0x0c); // LPDT: short package
	HDMI_WriteI2C_Byte(0x02, 0x04); // package length

	size = size / sizeof(struct cmd);
	for (i = 0; i < size; i++) {
		CMD_write(cmd[i].data_id, cmd[i].data1, cmd[i].data2);
	}
}
/********************************mode 1 end******************************/

void Dcs_Display_On(bool on)
{
	HDMI_WriteI2C_Byte(0xff, 0x81);
	HDMI_WriteI2C_Byte(0x0b, 0xfe);
	// HDMI_WriteI2C_Byte(0x0f,0xfe);
	HDMI_WriteI2C_Byte(0x0e, 0x6f); // 0xef
	HDMI_WriteI2C_Byte(0x0e, 0xff);

	HDMI_WriteI2C_Byte(0xff, 0x86);
	HDMI_WriteI2C_Byte(0x47, 0x01); // only port A lane0 send dcs cmd.
	HDMI_WriteI2C_Byte(0x48, 0x01);

	HDMI_WriteI2C_Byte(0xff, 0xd4);
	HDMI_WriteI2C_Byte(0x20, 0x2f);
	HDMI_WriteI2C_Byte(0x13, 0x00);
	Timer0_Delay1ms(100);

	DcsPktWrite(dcs0[0], (sizeof(dcs0) - 1), &dcs0[1]);

	DcsPktWrite(dcs1[0], (sizeof(dcs1) - 1), &dcs1[1]);

	Generic_Short_Write_1P(0x01, 0x00);
	Generic_Short_Write_1P(0x02, 0x00);
	Generic_Short_Write_1P(0x03, 0x54);
	Generic_Short_Write_1P(0x04, 0xD4);
	Generic_Short_Write_1P(0x05, 0x00);
	Generic_Short_Write_1P(0x06, 0x11);
	Generic_Short_Write_1P(0x07, 0x09);
	Generic_Short_Write_1P(0x08, 0x00);
	Generic_Short_Write_1P(0x09, 0x00);
	Generic_Short_Write_1P(0x0a, 0x00);
	Generic_Short_Write_1P(0x0b, 0x00);
	Generic_Short_Write_1P(0x0c, 0x00);
	Generic_Short_Write_1P(0x0d, 0x00);
	Generic_Short_Write_1P(0x0e, 0x00);
	Generic_Short_Write_1P(0x0f, 0x26);
	Generic_Short_Write_1P(0x10, 0x26);
	Generic_Short_Write_1P(0x11, 0x00);
	Generic_Short_Write_1P(0x12, 0x00);
	Generic_Short_Write_1P(0x13, 0x00);
	Generic_Short_Write_1P(0x14, 0x00);
	Generic_Short_Write_1P(0x15, 0x00);
	Generic_Short_Write_1P(0x16, 0x00);
	Generic_Short_Write_1P(0x17, 0x00);
	Generic_Short_Write_1P(0x18, 0x00);
	Generic_Short_Write_1P(0x19, 0x00);
	Generic_Short_Write_1P(0x1a, 0x00);
	Generic_Short_Write_1P(0x1b, 0x00);
	Generic_Short_Write_1P(0x1c, 0x00);
	Generic_Short_Write_1P(0x1d, 0x00);
	Generic_Short_Write_1P(0x1e, 0x40);
	Generic_Short_Write_1P(0x1f, 0x80);
	Generic_Short_Write_1P(0x20, 0x06);
	Generic_Short_Write_1P(0x21, 0x01);
	Generic_Short_Write_1P(0x22, 0x00);
	Generic_Short_Write_1P(0x23, 0x00);
	Generic_Short_Write_1P(0x24, 0x00);
	Generic_Short_Write_1P(0x25, 0x00);
	Generic_Short_Write_1P(0x26, 0x00);
	Generic_Short_Write_1P(0x27, 0x00);
	Generic_Short_Write_1P(0x28, 0x33);
	Generic_Short_Write_1P(0x29, 0x33);
	Generic_Short_Write_1P(0x2a, 0x00);
	Generic_Short_Write_1P(0x2b, 0x00);
	Generic_Short_Write_1P(0x2c, 0x00);
	Generic_Short_Write_1P(0x2d, 0x00);
	Generic_Short_Write_1P(0x2e, 0x00);
	Generic_Short_Write_1P(0x2f, 0x00);
	Generic_Short_Write_1P(0x30, 0x00);
	Generic_Short_Write_1P(0x31, 0x00);
	Generic_Short_Write_1P(0x32, 0x00);
	Generic_Short_Write_1P(0x33, 0x00);
	Generic_Short_Write_1P(0x34, 0x03);
	Generic_Short_Write_1P(0x35, 0x00);
	Generic_Short_Write_1P(0x36, 0x00);
	Generic_Short_Write_1P(0x37, 0x00);
	Generic_Short_Write_1P(0x38, 0x96);
	Generic_Short_Write_1P(0x39, 0x00);
	Generic_Short_Write_1P(0x3a, 0x00);
	Generic_Short_Write_1P(0x3b, 0x00);
	Generic_Short_Write_1P(0x3c, 0x00);
	Generic_Short_Write_1P(0x3d, 0x00);
	Generic_Short_Write_1P(0x3e, 0x00);
	Generic_Short_Write_1P(0x3f, 0x00);
	Generic_Short_Write_1P(0x40, 0x00);
	Generic_Short_Write_1P(0x41, 0x00);
	Generic_Short_Write_1P(0x42, 0x00);
	Generic_Short_Write_1P(0x43, 0x00);
	Generic_Short_Write_1P(0x44, 0x00);
	Generic_Short_Write_1P(0x50, 0x00);
	Generic_Short_Write_1P(0x51, 0x23);
	Generic_Short_Write_1P(0x52, 0x45);
	Generic_Short_Write_1P(0x53, 0x67);
	Generic_Short_Write_1P(0x54, 0x89);
	Generic_Short_Write_1P(0x55, 0xab);
	Generic_Short_Write_1P(0x56, 0x01);
	Generic_Short_Write_1P(0x57, 0x23);
	Generic_Short_Write_1P(0x58, 0x45);
	Generic_Short_Write_1P(0x59, 0x67);
	Generic_Short_Write_1P(0x5a, 0x89);
	Generic_Short_Write_1P(0x5b, 0xab);
	Generic_Short_Write_1P(0x5c, 0xcd);
	Generic_Short_Write_1P(0x5d, 0xef);
	Generic_Short_Write_1P(0x5e, 0x00);
	Generic_Short_Write_1P(0x5f, 0x0D);
	Generic_Short_Write_1P(0x60, 0x0D);
	Generic_Short_Write_1P(0x61, 0x0C);
	Generic_Short_Write_1P(0x62, 0x0C);
	Generic_Short_Write_1P(0x63, 0x0F);
	Generic_Short_Write_1P(0x64, 0x0F);
	Generic_Short_Write_1P(0x65, 0x0E);
	Generic_Short_Write_1P(0x66, 0x0E);
	Generic_Short_Write_1P(0x67, 0x08);
	Generic_Short_Write_1P(0x68, 0x02);
	Generic_Short_Write_1P(0x69, 0x02);
	Generic_Short_Write_1P(0x6a, 0x02);
	Generic_Short_Write_1P(0x6b, 0x02);
	Generic_Short_Write_1P(0x6c, 0x02);
	Generic_Short_Write_1P(0x6d, 0x02);
	Generic_Short_Write_1P(0x6e, 0x02);
	Generic_Short_Write_1P(0x6f, 0x02);
	Generic_Short_Write_1P(0x70, 0x14);
	Generic_Short_Write_1P(0x71, 0x15);
	Generic_Short_Write_1P(0x72, 0x06);
	Generic_Short_Write_1P(0x73, 0x02);
	Generic_Short_Write_1P(0x74, 0x02);
	Generic_Short_Write_1P(0x75, 0x0D);
	Generic_Short_Write_1P(0x76, 0x0D);
	Generic_Short_Write_1P(0x77, 0x0C);
	Generic_Short_Write_1P(0x78, 0x0C);
	Generic_Short_Write_1P(0x79, 0x0F);
	Generic_Short_Write_1P(0x7a, 0x0F);
	Generic_Short_Write_1P(0x7b, 0x0E);
	Generic_Short_Write_1P(0x7c, 0x0E);
	Generic_Short_Write_1P(0x7d, 0x08);
	Generic_Short_Write_1P(0x7e, 0x02);
	Generic_Short_Write_1P(0x7f, 0x02);
	Generic_Short_Write_1P(0x80, 0x02);
	Generic_Short_Write_1P(0x81, 0x02);
	Generic_Short_Write_1P(0x82, 0x02);
	Generic_Short_Write_1P(0x83, 0x02);
	Generic_Short_Write_1P(0x84, 0x02);
	Generic_Short_Write_1P(0x85, 0x02);
	Generic_Short_Write_1P(0x86, 0x14);
	Generic_Short_Write_1P(0x87, 0x15);
	Generic_Short_Write_1P(0x88, 0x06);
	Generic_Short_Write_1P(0x89, 0x02);
	Generic_Short_Write_1P(0x8A, 0x02);

	DcsPktWrite(dcs2[0], (sizeof(dcs2) - 1), &dcs2[1]);
	Generic_Short_Write_1P(0x6E, 0x3B);
	Generic_Short_Write_1P(0x6F, 0x57);
	Generic_Short_Write_1P(0x3A, 0x24);
	Generic_Short_Write_1P(0x8D, 0x1F);
	Generic_Short_Write_1P(0x87, 0xBA);
	Generic_Short_Write_1P(0xB2, 0xD1);
	Generic_Short_Write_1P(0x88, 0x0B);
	Generic_Short_Write_1P(0x38, 0x01);
	Generic_Short_Write_1P(0x39, 0x00);
	Generic_Short_Write_1P(0xB5, 0x07);
	Generic_Short_Write_1P(0x31, 0x75);
	Generic_Short_Write_1P(0x3B, 0x98);

	DcsPktWrite(dcs3[0], (sizeof(dcs3) - 1), &dcs3[1]);
	Generic_Short_Write_1P(0x22, 0x0A);
	Generic_Short_Write_1P(0x31, 0x09);
	Generic_Short_Write_1P(0x53, 0x7B);
	Generic_Short_Write_1P(0x55, 0x40);
	Generic_Short_Write_1P(0x50, 0x86);
	Generic_Short_Write_1P(0x51, 0x82);
	Generic_Short_Write_1P(0x60, 0x27);
	Generic_Short_Write_1P(0x62, 0x20);
	Generic_Short_Write_1P(0xA0, 0x00);
	Generic_Short_Write_1P(0xA1, 0x12);
	Generic_Short_Write_1P(0xA2, 0x20);
	Generic_Short_Write_1P(0xA3, 0x13);
	Generic_Short_Write_1P(0xA4, 0x14);
	Generic_Short_Write_1P(0xA5, 0x27);
	Generic_Short_Write_1P(0xA6, 0x1D);
	Generic_Short_Write_1P(0xA7, 0x1F);
	Generic_Short_Write_1P(0xA8, 0x7C);
	Generic_Short_Write_1P(0xA9, 0x1D);
	Generic_Short_Write_1P(0xAA, 0x2A);
	Generic_Short_Write_1P(0xAB, 0x6B);
	Generic_Short_Write_1P(0xAC, 0x1A);
	Generic_Short_Write_1P(0xAD, 0x18);
	Generic_Short_Write_1P(0xAE, 0x4E);
	Generic_Short_Write_1P(0xAF, 0x24);
	Generic_Short_Write_1P(0xB0, 0x2A);
	Generic_Short_Write_1P(0xB1, 0x4D);
	Generic_Short_Write_1P(0xB2, 0x5B);
	Generic_Short_Write_1P(0xB3, 0x23);
	Generic_Short_Write_1P(0xC0, 0x00);
	Generic_Short_Write_1P(0xC1, 0x13);
	Generic_Short_Write_1P(0xC2, 0x20);
	Generic_Short_Write_1P(0xC3, 0x12);
	Generic_Short_Write_1P(0xC4, 0x15);
	Generic_Short_Write_1P(0xC5, 0x28);
	Generic_Short_Write_1P(0xC6, 0x1C);
	Generic_Short_Write_1P(0xC7, 0x1E);
	Generic_Short_Write_1P(0xC8, 0x7B);
	Generic_Short_Write_1P(0xC9, 0x1E);
	Generic_Short_Write_1P(0xCA, 0x29);
	Generic_Short_Write_1P(0xCB, 0x6C);
	Generic_Short_Write_1P(0xCC, 0x1A);
	Generic_Short_Write_1P(0xCD, 0x19);
	Generic_Short_Write_1P(0xCE, 0x4D);
	Generic_Short_Write_1P(0xCF, 0x22);
	Generic_Short_Write_1P(0xD0, 0x2A);
	Generic_Short_Write_1P(0xD1, 0x4D);
	Generic_Short_Write_1P(0xD2, 0x5B);
	Generic_Short_Write_1P(0xD3, 0x23);

	DcsPktWrite(dcs4[0], (sizeof(dcs4) - 1), &dcs4[1]);

	//	DcsPktWrite(dcs2[0], (sizeof(dcs2) - 1), &dcs2[1]); //bist
	//	Generic_Short_Write_1P(0x2F,0x01);

	LPDT_Cmd_Write(cmd_301, sizeof(cmd_301));
	Timer0_Delay1ms(200);
	LPDT_Cmd_Write(cmd_302, sizeof(cmd_302));
	Timer0_Delay1ms(20);

	printf("\r\n send lcd initial code done");
}

//#endif
